package cn.chenhuanming.chuanzhi.util.entity.converter;

import cn.chenhuanming.chuanzhi.util.data.DataUtils;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * Created by Administrator on 2017-04-03.
 */
@Converter
public class LocalDateConverter implements AttributeConverter<LocalDate,String> {

    @Override
    public String convertToDatabaseColumn(LocalDate attribute) {
            return DataUtils.notNull(attribute, s->attribute.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")), s->null);
    }

    @Override
    public LocalDate convertToEntityAttribute(String dbData) {
        return DataUtils.notNull(dbData,s->LocalDate.parse(dbData),s->null);
    }
}
